package yfdc.douyin.download;

import android.app.Application;

import java.io.FileOutputStream;
import java.io.File;

public final class App extends Application implements Thread.UncaughtExceptionHandler {
    private final Thread.UncaughtExceptionHandler defaultUCHandler;

    public App() {
        super();
        NetWork.getInstance();
        Utils.getGlobalClient();
        defaultUCHandler = Thread.getDefaultUncaughtExceptionHandler();
    }

    @Override
    public final void onCreate() {
        super.onCreate();
        Thread.setDefaultUncaughtExceptionHandler(this);
    }

    @Override
    public void uncaughtException(final Thread thread, final Throwable exception) {
        if ((thread == null) || (exception == null)) return;
        String parent = Utils.notNull(this.getExternalFilesDir(null)).getAbsolutePath();
        java.io.File file = new java.io.File(parent + File.separatorChar + "err.log");
        boolean write = true;
        if (!file.exists()) {
            try {
                write = file.createNewFile();
            } catch (Throwable _var0_) {
                write = false;
            }
        }
        if (write) {
            try (FileOutputStream fos = new FileOutputStream(file, true)) {
                fos.write(("Exception in thread " + thread.getName() + ":" + exception + "\n")
                        .getBytes());
                for (StackTraceElement el : exception.getStackTrace()) {
                    fos.write(("  at " + el + "\n").getBytes());
                }
            } catch (Exception e) {
                Utils.logException(e, "App");
            }
        }
        final Thread.UncaughtExceptionHandler handler = this.defaultUCHandler;
        if (handler != null) {
            handler.uncaughtException(thread, exception);
        }
    }
}
